package com.example.myapplication2;

import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.GridLayout;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.example.myapplication2.POJO.Shop.Good;
import com.example.myapplication2.dataBase.ShopHelper;
import org.w3c.dom.Text;

import java.util.List;

public class ShopMallActivity extends AppCompatActivity implements View.OnClickListener {
    private TextView title;
    private GridLayout gridLayout;
    private ShopHelper shopHelper;
    private TextView title_count;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shop_mall);
        title = findViewById(R.id.shopTitle);
        title_count = findViewById(R.id.title_count);
        title.setText("商城");
        gridLayout = findViewById(R.id.goodList);
        shopHelper = ShopHelper.getInstance(this);
        //加载数据库里面的商品数据
        showGoods();
        //设置按键监听
        findViewById(R.id.title_back).setOnClickListener(this);
    }

    private void showGoods() {
        Log.d("info", "加载视图");
        Long i=0L;
        //移除子视图
        gridLayout.removeAllViews();
        List<Good> goods = shopHelper.GoodAll();
        for (Good good : goods) {
            //查找自定义布局文件
            View inflate = LayoutInflater.from(this).inflate(R.layout.mall_good, null);
            TextView mail_good_img = inflate.findViewById(R.id.mail_good_img);
            mail_good_img.setText(good.getImg());
            TextView mail_good_name = inflate.findViewById(R.id.mail_good_name);
            mail_good_name.setText(good.getGoodName());
            TextView mail_good_introduce = inflate.findViewById(R.id.mail_good_introduce);
            mail_good_introduce.setText(good.getIntroduce());
            TextView mail_good_price = inflate.findViewById(R.id.mail_good_price);
            mail_good_price.setText(good.getPrice().toString());
            i=i+1L;
            long l = i % 4;
            switch ((int) l){
                case 1:
                    //设置颜色
                    inflate.setBackgroundColor(Color.WHITE);
                    break;
                case 0:
                    inflate.setBackgroundColor(Color.WHITE);
                    break;
                default:
                    inflate.setBackgroundColor(Color.GRAY);
                    break;
            }
            //设置点击事件
            inflate.findViewById(R.id.mail_good_add).setOnClickListener(view -> {
                addToCar(good);
            });
            gridLayout.addView(inflate);
        }
        //更新标题的计数
        Long count = shopHelper.Count();
        title_count.setText(count.toString());
    }

    private void addToCar(Good good) {
        shopHelper.addToCar(good);
        //更新标题的计数
        Long count = shopHelper.Count();
        title_count.setText(count.toString());
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.title_back:
                finish();
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        showGoods();
    }
}